//
//  OrderDetailBottomView.m
//  JiaoX
//
//  Created by yinqy on 15/1/20.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "OrderDetailBottomView.h"

#define CANCEL_BTN_WIDTH [DimensUtils getScaleX:100]
#define BOTTOM_FONT_SIZE [DimensUtils getScaleX:15]
#define CHANGEPAY_BTN_WIDTH [DimensUtils getScaleX:131]
#define SUNSINGLE_BTN_WIDTH [DimensUtils getScaleX:125]


@interface OrderDetailBottomView()
{
    UIButton *_cancelBtn;
    UIButton *_changePayBtn;
    UIButton *_orderDeliveryBtn;
    UIButton *_sunSingleBtn;
}

@end
@implementation OrderDetailBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0 , JXScreenW, 49);
        self.backgroundColor = JXColorAlpha(255, 255, 255,220);
        UIView *spliteView = [[UIView alloc]init];
        [spliteView setBackgroundColor:JXColor(153, 153, 153)];
        [spliteView setFrame:CGRectMake(0, 0, self.width, 0.5)];
        [self addSubview:spliteView];
        _cancelBtn = [self setBtnWithFrame:CGRectMake(JXPublicInset, 12/2, CANCEL_BTN_WIDTH, self.height-12) title:@"取消订单" color:[UIColor blackColor]];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _changePayBtn = [self setBtnWithFrame:CGRectMake(JXScreenW-CHANGEPAY_BTN_WIDTH-JXPublicInset, 12/2, CHANGEPAY_BTN_WIDTH, self.height-12) title:@"改为在线支付" color:JXThemeColor];
        [_changePayBtn addTarget:self action:@selector(changePayBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _orderDeliveryBtn = [self setBtnWithFrame:CGRectMake((JXScreenW-CHANGEPAY_BTN_WIDTH)/2, 12/2, CHANGEPAY_BTN_WIDTH, self.height-12) title:@"查看物流信息" color:[UIColor grayColor]];
        [_orderDeliveryBtn addTarget:self action:@selector(orderDeliveryBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _orderDeliveryBtn.hidden = YES;
        
        _sunSingleBtn = [self setBtnWithFrame:CGRectMake((JXScreenW-SUNSINGLE_BTN_WIDTH)/2, 12/2, SUNSINGLE_BTN_WIDTH, self.height-12) title:@"晒单" color:[UIColor blackColor]];
        [_sunSingleBtn addTarget:self action:@selector(sunSingleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _sunSingleBtn.hidden = YES;
    }
    return self;
}

-(void)setPayMode:(int)payMode status:(int)status{
    //支付方式不为货到付款，支付状态为待支付
    if (payMode!=1 && status == 1) {
        [_changePayBtn setTitle:@"立即支付" forState:UIControlStateNormal];
    }
}

-(void)showCancelBtn:(int)payMode status:(int)status :(BOOL)orderDeliveryBtn{
    [self setPayMode:payMode status:status];
    //如果状态为待支付或者待确认
    if(status == 1){
        _cancelBtn.hidden = NO;
        _changePayBtn.hidden = NO;
        _orderDeliveryBtn.hidden = YES;
    }else if(status == 4){//代表订单已完成
        [_sunSingleBtn setHidden:NO];
        
        _cancelBtn.hidden = YES;
        _changePayBtn.hidden = YES;
        _orderDeliveryBtn.hidden = YES;
    }else{
        if (orderDeliveryBtn) {
            _orderDeliveryBtn.hidden = NO;
            _cancelBtn.hidden = YES;
            _changePayBtn.hidden = YES;
        }else{
            self.hidden = YES;
        }
    }
}

-(void)cancelBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(cancelBtnClick:)]) {
        [_delegate cancelBtnClick:btn];
    }
}

-(void)changePayBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(changePayBtnClick:)]) {
        [_delegate changePayBtnClick:btn];
    }
}

-(void)orderDeliveryBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(orderDeliveryBtnClick:)]) {
        [_delegate orderDeliveryBtnClick:btn];
    }
}

-(void)sunSingleBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(sunSingleBtnClick:)]) {
        [_delegate sunSingleBtnClick:btn];
    }
}

-(UIButton *)setBtnWithFrame:(CGRect)frame title:(NSString *)title color:(UIColor *)color{
    UIButton *btn =[[UIButton alloc]initWithFrame:frame];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:BOTTOM_FONT_SIZE]];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn.layer setCornerRadius:5];
    [btn.layer setBorderColor:color.CGColor];
    [btn.layer setBorderWidth:1];
    [self addSubview:btn];
    return btn;
}

@end
